package com.tianji.learning.mq;

import cn.hutool.core.collection.CollectionUtil;
import com.tianji.api.client.course.CourseClient;
import com.tianji.api.dto.trade.OrderBasicDTO;
import com.tianji.common.constants.MqConstants;
import com.tianji.learning.service.ILearningLessonService;
import lombok.RequiredArgsConstructor;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

/**
 * TODO
 *
 * @Author wxy
 * @Date 2024/12/3 11:53
 */
@Component
@RequiredArgsConstructor
public class LessonChangeListener {
    private final CourseClient courseClient;
    private final ILearningLessonService learningLessonService;
    /**
     * 监听订单支付或课程报名的消息
     * @param dto
     */
    @RabbitListener(bindings = @QueueBinding(value = @Queue(value = "learning.lesson.pay.queue",durable = "true"),
            exchange = @Exchange(name = MqConstants.Exchange.ORDER_EXCHANGE, type = ExchangeTypes.TOPIC),
            key =MqConstants.Key.ORDER_PAY_KEY ))
    public void listenLessonPay(OrderBasicDTO dto){
        System.out.println(dto);
        if(dto==null||dto.getUserId()==null|| CollectionUtil.isEmpty(dto.getCourseIds())){
            return;
        }
        learningLessonService.savelession(dto.getUserId(),dto.getCourseIds());
    }

    /**
     * 监听退款信息
     * @param dto
     */
    @RabbitListener(bindings = @QueueBinding(value = @Queue(value = "learning.lesson.refund.queue",durable = "true"),
            exchange = @Exchange(name = MqConstants.Exchange.ORDER_EXCHANGE, type = ExchangeTypes.TOPIC),
            key =MqConstants.Key.ORDER_REFUND_KEY))
    public void listerlessonrefund(OrderBasicDTO dto){
        System.out.println(dto);
        if(dto==null||dto.getUserId()==null|| CollectionUtil.isEmpty(dto.getCourseIds())){
            return;
        }
        learningLessonService.deletelession(dto.getUserId(),dto.getCourseIds());
    }

}
